package com.liesheng.haylou.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.liesheng.haylou.db.entity.StringConverter;
import java.util.List;

import com.liesheng.haylou.db.entity.BoundedDevice;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "BOUNDED_DEVICE".
*/
public class BoundedDeviceDao extends AbstractDao<BoundedDevice, String> {

    public static final String TABLENAME = "BOUNDED_DEVICE";

    /**
     * Properties of entity BoundedDevice.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Address = new Property(0, String.class, "address", true, "ADDRESS");
        public final static Property Name = new Property(1, String.class, "name", false, "NAME");
        public final static Property Type = new Property(2, int.class, "type", false, "TYPE");
        public final static Property Latitude = new Property(3, double.class, "latitude", false, "LATITUDE");
        public final static Property Longitude = new Property(4, double.class, "longitude", false, "LONGITUDE");
        public final static Property Broadcast_count = new Property(5, String.class, "broadcast_count", false, "BROADCAST_COUNT");
        public final static Property Locations = new Property(6, String.class, "locations", false, "LOCATIONS");
    }

    private final StringConverter locationsConverter = new StringConverter();

    public BoundedDeviceDao(DaoConfig config) {
        super(config);
    }
    
    public BoundedDeviceDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"BOUNDED_DEVICE\" (" + //
                "\"ADDRESS\" TEXT PRIMARY KEY NOT NULL ," + // 0: address
                "\"NAME\" TEXT," + // 1: name
                "\"TYPE\" INTEGER NOT NULL ," + // 2: type
                "\"LATITUDE\" REAL NOT NULL ," + // 3: latitude
                "\"LONGITUDE\" REAL NOT NULL ," + // 4: longitude
                "\"BROADCAST_COUNT\" TEXT," + // 5: broadcast_count
                "\"LOCATIONS\" TEXT);"); // 6: locations
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOUNDED_DEVICE\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, BoundedDevice entity) {
        stmt.clearBindings();
 
        String address = entity.getAddress();
        if (address != null) {
            stmt.bindString(1, address);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(2, name);
        }
        stmt.bindLong(3, entity.getType());
        stmt.bindDouble(4, entity.getLatitude());
        stmt.bindDouble(5, entity.getLongitude());
 
        String broadcast_count = entity.getBroadcast_count();
        if (broadcast_count != null) {
            stmt.bindString(6, broadcast_count);
        }
 
        List locations = entity.getLocations();
        if (locations != null) {
            stmt.bindString(7, locationsConverter.convertToDatabaseValue(locations));
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, BoundedDevice entity) {
        stmt.clearBindings();
 
        String address = entity.getAddress();
        if (address != null) {
            stmt.bindString(1, address);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(2, name);
        }
        stmt.bindLong(3, entity.getType());
        stmt.bindDouble(4, entity.getLatitude());
        stmt.bindDouble(5, entity.getLongitude());
 
        String broadcast_count = entity.getBroadcast_count();
        if (broadcast_count != null) {
            stmt.bindString(6, broadcast_count);
        }
 
        List locations = entity.getLocations();
        if (locations != null) {
            stmt.bindString(7, locationsConverter.convertToDatabaseValue(locations));
        }
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public BoundedDevice readEntity(Cursor cursor, int offset) {
        BoundedDevice entity = new BoundedDevice( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // address
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // name
            cursor.getInt(offset + 2), // type
            cursor.getDouble(offset + 3), // latitude
            cursor.getDouble(offset + 4), // longitude
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // broadcast_count
            cursor.isNull(offset + 6) ? null : locationsConverter.convertToEntityProperty(cursor.getString(offset + 6)) // locations
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, BoundedDevice entity, int offset) {
        entity.setAddress(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setType(cursor.getInt(offset + 2));
        entity.setLatitude(cursor.getDouble(offset + 3));
        entity.setLongitude(cursor.getDouble(offset + 4));
        entity.setBroadcast_count(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setLocations(cursor.isNull(offset + 6) ? null : locationsConverter.convertToEntityProperty(cursor.getString(offset + 6)));
     }
    
    @Override
    protected final String updateKeyAfterInsert(BoundedDevice entity, long rowId) {
        return entity.getAddress();
    }
    
    @Override
    public String getKey(BoundedDevice entity) {
        if(entity != null) {
            return entity.getAddress();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(BoundedDevice entity) {
        return entity.getAddress() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
}
